/* 
 * Project: FlarDetector
 *
 * Copyright 2009, Longfellow Chen
 * --------------------------------------------------------------------------------
 * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
 *	http://www.libspark.org/wiki/saqoosha/FLARToolKit
 * FLARToolkit is Copyright (C)2008 Saqoosha,
 * and is ported from NYARToolkit, which is ported from ARToolkit.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * For further information please contact:
 *	<fellong0307@gmail.com>
 *	http://fellong.blogspot.com/
 * 
 */
 
 package com.fellong.flash.flar.object.ext
{
	import com.fellong.flash.flar.detector.FlarDetector;
	import com.fellong.flash.flar.object.FlarObject;

	import flash.events.Event;

	import org.papervision3d.core.material.TriangleMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;

	public class FlarARisCube extends FlarObject
	{
		protected var _up:FlarDetector;
		protected var _down:FlarDetector;
		protected var _left:FlarDetector;
		protected var _right:FlarDetector;
		protected var _back:FlarDetector;
		protected var _front:FlarDetector;

		protected var _cube_space:Number;
		
		private var _erase_cube:Cube=null;
				
		public function FlarARisCube( cube_space:Number=3, erase_green_cube:Boolean=true )
		{
			super();
			_cube_space = cube_space;
							
			//_up = new FlarDetector();
			_up = this._detector;
			_front = new FlarDetector();
			_right = new FlarDetector();
			_left = new FlarDetector();
			_back = new FlarDetector();
			_down = new FlarDetector();
			
			//_up.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			_front.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			_right.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			_left.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			_back.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			_down.addEventListener( FlarDetector.MARKER_UPDATED, onMARKER_UPDATED );
			
			//_up.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );
			_front.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );
			_right.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );
			_left.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );
			_back.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );
			_down.addEventListener( FlarDetector.MARKER_REMOVED, onMARKER_REMOVED );

			if( erase_green_cube )
				_up.addEventListener( FlarDetector.REGIST, onREGIST );
		}
		
		private function onREGIST( event:Event ):void
		{
			this.removeChild( _erase_cube );
			var fmat:TriangleMaterial;
			fmat = new ColorMaterial(0x00FF00);
			fmat.doubleSided = true;
			var cSize:uint = _up.codeWidth+_cube_space*2;
			_erase_cube = new Cube(new MaterialsList( { all: fmat } ), cSize,cSize,cSize);
			_erase_cube.z -= cSize/2;
			this.addChild(_erase_cube);
		}

		override protected function onMARKER_UPDATED( event:Event ):void
		{
			/// calculate center point
			var off:Number = _up.codeWidth/2+_cube_space;

			if( _up.detected )
			{
				this.pivot.rotationX = 90;
				this.pivot.rotationY = 0;
				this.pivot.rotationZ = 90;
				this.pivot.x = 0; 
				this.pivot.y = 0; 
				this.pivot.z = 0; 
				this.setTransformMatrix( this._up.transmat );
			}else if( _down.detected )
			{
				this.pivot.rotationX = -90;
				this.pivot.rotationY = 0;
				this.pivot.rotationZ = 90;
				this.pivot.x = 0; 
				this.pivot.y = 0; 
				this.pivot.z = -off*2; 
				this.setTransformMatrix( this._down.transmat );
			}else if( _right.detected )
			{
				this.pivot.rotationX = 180;
				this.pivot.rotationY = 270;
				this.pivot.rotationZ = 90;
				this.pivot.x = off; 
				this.pivot.y = 0; 
				this.pivot.z = -off; 
				this.setTransformMatrix( this._right.transmat );
			}else if( _left.detected )
			{
				this.pivot.rotationX = 0;
				this.pivot.rotationY = 90;
				this.pivot.rotationZ = 270;
				this.pivot.x = off; 
				this.pivot.y = 0; 
				this.pivot.z = -off; 
				this.setTransformMatrix( this._left.transmat );
			}else if( _front.detected )
			{
				this.pivot.rotationX = 0;
				this.pivot.rotationY = 180;
				this.pivot.rotationZ = -90;
				this.pivot.x = off; 
				this.pivot.y = 0; 
				this.pivot.z = -off; 
				this.setTransformMatrix( this._front.transmat );
			}else if( _back.detected )
					{
				this.pivot.rotationX = 0;
				this.pivot.rotationY = 0;
				this.pivot.rotationZ = -90;
				this.pivot.x = off; 
				this.pivot.y = 0; 
				this.pivot.z = -off; 
				this.setTransformMatrix( this._back.transmat );
			}
			
			this.visible = true;
		}
		
		override protected function onMARKER_REMOVED( event:Event ):void
		{
			if( !_up.detected 
				&& !_down.detected 
				&& !_right.detected 
				&& !_left.detected
				&& !_front.detected
				&& !_back.detected )
			{
				this.visible = false;
			}
		}

		public function get up_detector():FlarDetector
		{
			return _up;
		}
		
		public function get down_detector():FlarDetector
		{
			return _down;
		}
		
		public function get right_detector():FlarDetector
		{
			return _right;
		}
		
		public function get left_detector():FlarDetector
		{
			return _left;
		}
		
		public function get front_detector():FlarDetector
		{
			return _front;
		}
		
		public function get back_detector():FlarDetector
		{
			return _back;
		}
		
		override public function set confidence( v:Number ):void
		{
			_up._confidence = v;
			_down._confidence = v;
			_left._confidence = v;
			_right._confidence = v;
			_front._confidence = v;
			_back._confidence = v;
		}
		
		override public function set delay( v:Number ):void
		{
			_up._delay = v;
			_down._delay = v;
			_left._delay = v;
			_right._delay = v;
			_front._delay = v;
			_back._delay = v;
		}
		
		override public function set smooth( v:Number ):void
		{
			_up._smooth = v;
			_down._smooth = v;
			_left._smooth = v;
			_right._smooth = v;
			_front._smooth = v;
			_back._smooth = v;
		}
	}
}